/***
This do-file plots a binscatter of changes in low-wage employment vs
median rent by county and a binscatter of changes in low-wage employment vs median
rent by county.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Employment"

* Create required subfolders
cap mkdir "${root}/results"
cap mkdir "${root}/results/Employment"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

* Erase output numbers
cap erase "${root}/results/paper numbers/`category'/Low-Wage Employment vs Median Rent (county).yaml"
cap erase "${root}/results/paper numbers/`category'/Change in Low-Income Employment vs. Median Rent, by County.yaml"

*-------------------------------------------------------------------------------
* Changes in low-wage employment vs median 2-bedroom rent
*-------------------------------------------------------------------------------

* Load employment data
project, uses("${root}/data/web/data/Employment - County - Weekly.csv")
import delimited "${root}/data/web/data/Employment - County - Weekly.csv", clear

gen date = mdy(month, day_endofweek, year)

rename emp_incq1 emp_index

* Make a percentage change
replace emp_index =  100 * emp_index

* Take mean of month
gegen temp = mean(emp_index) if year == 2020 & month == 4, by(countyfips)
	gegen change_apr_2020 = mean(temp), by(countyfips)
	drop temp

gegen temp = mean(emp_index) if year == 2020 & month == 5, by(countyfips)
	gegen change_may_2020 = mean(temp), by(countyfips)
	drop temp

gegen temp = mean(emp_index) if year == 2020 & month == 6, by(countyfips)
	gegen change_jun_2020 = mean(temp), by(countyfips)
	drop temp

gegen temp = mean(emp_index) if year == 2020 & month == 7, by(countyfips)
	gegen change_jul_2020 = mean(temp), by(countyfips)
	drop temp

* Merge on covariates
rename countyfips county_fips
project, uses("${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta")
merge m:1 county_fips using "${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta", keepusing(med_2br_2014_2018_est pop_2014_2018_est) keep(3) nogen
rename county_fips countyfips

rename (med_2br_2014_2018_est pop_2014_2018_est) (median_2br_rent_2018 pop_2018)

gcollapse change_* median_2br_rent_2018 pop_2018, by(countyfips)

* Run regressions
reg change_jul_2020 median_2br_rent_2018 [w = pop_2018], r
local se = round(_se[median_2br_rent_2018], 0.00001) * 1000
local beta = round(_b[median_2br_rent_2018], 0.00001) * 1000
local se = string(`se', "%3.2f")
local beta = string(`beta', "%3.2f")
local unit = "/$1000"

*------------------------------------------------------------------------------*
* Plot
*------------------------------------------------------------------------------*

binscatter change_jul_2020 median_2br_rent_2018 [w = pop_2018] ///
	, ///
	${title_`version'} ///
	aspectratio(0.5) ///
	ytitle("Change in Employment (%)" "from January to July 2020") ///
	xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
	text(-25.3 500 "Slope = `beta'%`unit' (s.e. = `se')", place(ne) just(left) color(gs8) size(medlarge)) ///
	ysc(range(-25 -15)) xsc(ra(500 2050)) ///
	ylab(-15 "-15%" -20 "-20%" -25 "-25%", nogrid) ///
	xlab(500 "$500" 1000 "$1,000" 1500 "$1,500" 2000 "$2,000") ///
	legend(off)

	oi_graph_export "${root}/results/Employment/Low-wage Employment vs Median Rent by County Binscatter", type(${fig_type})

* Black and white version for QJE
binscatter change_jul_2020 median_2br_rent_2018 [w = pop_2018] ///
	,  mcolor(gs0) lcolor(gs8) ///
	${title_`version'} ///
	aspectratio(0.5) ///
	ytitle("Change in Employment (%)" "from January to July 2020") ///
	xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
	text(-25.3 500 "Slope = `beta'%`unit' (s.e. = `se')", place(ne) just(left) color(gs8) size(medlarge)) ///
	ysc(range(-25 -15)) xsc(ra(500 2050)) ///
	ylab(-15 "-15%" -20 "-20%" -25 "-25%", nogrid) ///
	xlab(500 "$500" 1000 "$1,000" 1500 "$1,500" 2000 "$2,000") ///
	legend(off)

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_4c.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_4c.svg")

yamlout using "${root}/results/paper numbers/`category'/Low-Wage Employment vs Median Rent (county).yaml", ///
	key("emp_rent_slope") ///
	comment("Slope (%/$1000)") ///
	value(`beta') fmt(%9.2f)
yamlout using "${root}/results/paper numbers/`category'/Low-Wage Employment vs Median Rent (county).yaml", ///
	key("emp_rent_se") ///
	comment("SE") ///
	value(`se') fmt(%9.2f)

*-------------------------------------------------------------------------------
* More stats
*-------------------------------------------------------------------------------
gquantiles rent_ventile = median_2br_rent_2018 [w = pop_2018] if !mi(change_jul_2020), xtile nq(20)

sum change_jul_2020 [w = pop_2018] if rent_ventile == 1
local emp_jul_2020_rent_p5: di %3.1f abs(r(mean))
sum change_jul_2020 [w = pop_2018] if rent_ventile == 20
local emp_jul_2020_rent_p100: di %3.1f abs(r(mean))

di `emp_jul_2020_rent_p5'
di `emp_jul_2020_rent_p100'

yamlout using "${root}/results/paper numbers/`category'/Low-Wage Employment vs Median Rent (county).yaml", ///
	key("emp_jul_2020_rent_p5") ///
	comment("Q1 emp change in first rent ventile in Jul 2020") ///
	value(`emp_jul_2020_rent_p5') fmt(%9.1f)
yamlout using "${root}/results/paper numbers/`category'/Low-Wage Employment vs Median Rent (county).yaml", ///
	key("emp_jul_2020_rent_p100") ///
	comment("Q1 emp change in last rent ventile in Jul 2020") ///
	value(`emp_jul_2020_rent_p100') fmt(%9.1f)

project, creates("${root}/results/paper numbers/`category'/Low-Wage Employment vs Median Rent (county).yaml")

*-------------------------------------------------------------------------------
* Changes in low-wage employment vs 2-bedroom median rent by county
*-------------------------------------------------------------------------------

* Load employment data
project, uses("${root}/data/web/data/Employment - County - Weekly.csv")
import delimited "${root}/data/web/data/Employment - County - Weekly.csv", clear

gen date = mdy(month, day_endofweek, year)

rename emp_incq1 emp_index
replace emp_index =  100 * emp_index // % change

* Take mean of month
gegen temp = mean(emp_index) if year == 2020 & month == 4, by(countyfips)
	gegen change_apr = mean(temp), by(countyfips)
	drop temp

gegen temp = mean(emp_index) if year == 2020 & month == 5, by(countyfips)
	gegen change_may = mean(temp), by(countyfips)
	drop temp

gegen temp = mean(emp_index) if year == 2020 & month == 6, by(countyfips)
	gegen change_jun = mean(temp), by(countyfips)
	drop temp

gegen temp = mean(emp_index) if year == 2020 & month == 7, by(countyfips)
	gegen change_jul = mean(temp), by(countyfips)
	drop temp

gegen temp = mean(emp_index) if year == 2020 & month == 10, by(countyfips)
	gegen change_oct = mean(temp), by(countyfips)
	drop temp

gegen temp = mean(emp_index) if year == 2020 & month == 12, by(countyfips)
	gegen change_dec = mean(temp), by(countyfips)
	drop temp

* Alternatively: Take mean as of Jan 2021
gen jan2021 = emp_index if inrange(date, mdy(1, 4, 2021), mdy(1, 31, 2021))
gegen change_jan2021 = mean(jan2021), by(countyfips)

* Collapse
gcollapse (mean) change_apr change_may change_jul change_oct change_dec change_jan2021, by(countyfips)

* Merge on covariates
rename countyfips county_fips
project, uses("${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta")
merge m:1 county_fips using "${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta", keepusing(med_2br_2014_2018_est pop_2014_2018_est) keep(3) nogen
rename county_fips countyfips
rename (med_2br_2014_2018_est pop_2014_2018_est) (median_2br_rent_2018 pop_2018)

* Reshape
keep change* median_2br_rent_2018 pop_2018 countyfips
reshape long change, i(countyfips) j(period) string

* Run regressions
reg change median_2br_rent_2018 if period == "_may" [w = pop_2018], r
local display_beta_may : di %4.2f _b[median_2br_rent_2018] * 1000
local display_se_may : di %4.2f _se[median_2br_rent_2018] * 1000
reg change median_2br_rent_2018 if period == "_jul" [w = pop_2018], r
local display_beta_july : di %4.2f _b[median_2br_rent_2018] * 1000
local display_se_july : di %4.2f _se[median_2br_rent_2018] * 1000
reg change median_2br_rent_2018 if period == "_oct" [w = pop_2018], r
local display_beta_oct : di %4.2f _b[median_2br_rent_2018] * 1000
local display_se_oct : di %4.2f _se[median_2br_rent_2018] * 1000
reg change median_2br_rent_2018 if period == "_dec" [w = pop_2018], r
local display_beta_dec : di %4.2f _b[median_2br_rent_2018] * 1000
local display_se_dec : di %4.2f _se[median_2br_rent_2018] * 1000
reg change median_2br_rent_2018 if period == "_jan2021" [w = pop_2018], r
local display_beta_jan2021 : di %4.2f _b[median_2br_rent_2018] * 1000
local display_se_jan2021 : di %4.2f _se[median_2br_rent_2018] * 1000

replace period = "1" if period == "_jul"
replace period = "2" if period == "_dec"

*-------------------------------------------------------------------------------
* Plot
*------------------------------------------------------------------------------

binscatter change median_2br_rent_2018 [w = pop_2018] ///
		if inlist(period, "1", "2") ///
		, ///
		by(period) ///
		${title_`version'} ///
		ytitle("Change in Employment (%)" "Relative to January 2020") ///
		xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
		ylab( -15 "-15%" -20 "-20%" -25 "-25%" -30 "-30%", nogrid) ///
		xlab(500(500)2000) ///
		colors(oi1 oi2) ///
		msymbols(circle diamond) ///
		text(-15 1800 "Change from Jan-Jul" "Slope = `display_beta_july'%/$1000" "(s.e. = `display_se_july')", color(oi1) size(medsmall) j(left)) ///
		text(-27 780  "Change from Jan-Dec" "Slope = `display_beta_dec'%/$1000" "(s.e. = `display_se_dec')", color(oi2) size(medsmall) j(left)) 	///
		legend(off)
oi_graph_export "${root}/results/Employment/Association between changes in Low-wage Employment vs Median Rent by County", type(${fig_type})

yamlout using "${root}/results/paper numbers/`category'/Change in Low-Income Employment vs. Median Rent, by County.yaml", ///
	key("emp_rent_janjul_slope") ///
	comment("Jan-Jul Slope (%/$1000)") ///
	value(`display_beta_july') fmt(%9.2f)
yamlout using "${root}/results/paper numbers/`category'/Change in Low-Income Employment vs. Median Rent, by County.yaml", ///
	key("emp_rent_janjul_se") ///
	comment("Jan-Jul SE") ///
	value(`display_se_july') fmt(%9.2f)
yamlout using "${root}/results/paper numbers/`category'/Change in Low-Income Employment vs. Median Rent, by County.yaml", ///
	key("emp_rent_jandec_slope") ///
	comment("Jan-Dec Slope (%/$1000)") ///
	value(`display_beta_oct') fmt(%9.2f)
yamlout using "${root}/results/paper numbers/`category'/Change in Low-Income Employment vs. Median Rent, by County.yaml", ///
	key("emp_rent_jandec_se") ///
	comment("Jan-Dec SE") ///
	value(`display_se_oct') fmt(%9.2f)

project, creates("${root}/results/paper numbers/`category'/Change in Low-Income Employment vs. Median Rent, by County.yaml")
